<template>
	<view class="tab">
		<view class="tab-field">
			<view class="tab-field-pwd">
				<van-field
					label="原二级密码"
					type="password"
					placeholder="请输入原二级密码"
					clearable
					title-width="180rpx"
					:value="from.pwd"
					:border="false"
					@change="cPassword"
				/>
			</view>
		
			<van-cell-group>
			   <van-field
					label="新二级密码"
					type="password"
					placeholder="请输入新二级密码"
					clearable
					title-width="180rpx"
					:value="from.payPassword"
					@change="cPasswordT"
			  />
			   <van-field
					label="确认二级密码"
					type="password"
					title-width="180rpx"
					:value="from.payPassword2"
					placeholder="确认新二级密码"
					clearable
					:border="false"
					@change="cPasswordT2"
			  />
			</van-cell-group>
		</view>
		<view class="tab-btn-wrap">
			<button class="base-btn"  @tap="onSubmit3">提交</button>
		</view>
	</view>
</template>

<script>
import * as reg from '../../../../common/utils/reg.js';
import { mapActions, mapGetters } from 'vuex';
export default {
	props: {
		activeTAB: {
			type: Number
		}
	},
	data() {
		return {
			loading: false,
			from: {
				pwd: '',
				payPassword: '',
				payPassword2: ''
			}
		};
	},
	computed: {
		...mapGetters({
			userInfo: 'getUserInfo'
		})
	},
	methods: {
		onSubmit3 () {
			if (this.activeTAB !== 2) return
			if (this.loading) return
			const regSubmit = reg.regSubmit(this.from)
			if (this.from.pwd.length < 1) {
				uni.showToast({
					title: '请输入原二级密码', 
					icon: 'none'
				});
				return
			}
			if (regSubmit) {
				uni.showToast({
					title: regSubmit, 
					icon: 'none'
				});
				return
			}
			uni.showLoading({ title: '' });
			this.loading = true
			this.$http.resetTPwd({
				npwd: this.from.payPassword,
				pwd: this.from.pwd
			}).then(res => {
				const data = res.data
				this.loading = false
				if (data.success) {
					uni.setStorage({
						key: 'STOREKEY_LOGIN',
						data: '',
						success: () => {
							uni.showToast({
								title: '修改成功，请重新登录！',
								icon: 'none'
							});
							this.onClear()
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/user/login/index'
								});
							}, 500)
						}
					})
					return
				}
				uni.showToast({
					title: data.errMsg,
					icon: 'none'
				})
			}).catch(err => {
				this.loading = false
			})
		},
		onClear () {
			this.$set(this.from, 'pwd', '')
			this.$set(this.from, 'payPassword', '')
			this.$set(this.from, 'payPassword2', '')
		},
		cPassword(e) {
			this.from.pwd = e.detail
		},
		cPasswordT(e) {
			this.from.payPassword = e.detail
		},
		cPasswordT2(e) {
			this.from.payPassword2 = e.detail
		}
	}
};
</script>

<style lang="scss" scoped>
	.tab {
		padding: 40upx 0;
		&-field {
			margin-bottom: 40upx;
			&-pwd{margin-bottom: 20upx;}
		}
		&-btn-wrap {
			padding: 40upx 60upx;
		}
	}
</style>
